﻿#include "WRDateTime.h"
#include <QDateTime>

//static const WR32s g_daysToMonth365[] = { 0, 0x1f, 0x3b, 90, 120, 0x97, 0xb5, 0xd4, 0xf3, 0x111, 0x130, 0x14e, 0x16d };
//static const WR32s g_daysToMonth366[] = { 0, 0x1f, 60, 0x5b, 0x79, 0x98, 0xb6, 0xd5, 0xf4, 0x112, 0x131, 0x14f, 0x16e };
static const WR32s g_daysToMonth365[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
static const WR32s g_daysToMonth366[] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };

//获取日期的某一部分。
WR32s WRDateTime::getDatePart(WR32s part) const
{
    WR32s num2 = (WR32s)(m_ticks / WRTICKS_PER_DAY);
    WR32s num3 = num2 / WRDAYS_PER_400YEARS;
    num2 -= num3 * WRDAYS_PER_400YEARS;
    WR32s num4 = num2 / WRDAYS_PER_100YEARS;
    if (num4 == 4)
    {
        num4 = 3;
    }
    num2 -= num4 * WRDAYS_PER_100YEARS;
    WR32s num5 = num2 / WRDAYS_PER_4YEARS;
    num2 -= num5 * WRDAYS_PER_4YEARS;
    WR32s num6 = num2 / WRDAYS_PER_YEAR;
    if (num6 == 4)
    {
        num6 = 3;
    }
    if (part == 0)
    {
        return (((((num3 * 400) + (num4 * 100)) + (num5 * 4)) + num6) + 1);
    }
    num2 -= num6 * WRDAYS_PER_YEAR;
    if (part == 1)
    {
        return (num2 + 1);
    }
    const WR32s* numArray = ((num6 == 3) && ((num5 != 24) || (num4 == 3))) ? g_daysToMonth366 : g_daysToMonth365;
    WR32s index = num2 >> 6;
    while (num2 >= numArray[index])
    {
        index++;
    }
    if (part == 2)
    {
        return index;
    }
    return ((num2 - numArray[index - 1]) + 1);
}

//构造日期时间。
bool WRDateTime::create(WR32s year, WR32s month, WR32s day)
{
    if (((year >= 1) && (year <= 9999)) && ((month >= 1) && (month <= 12)))
    {
        const WR32s *numArray = isLeapYear(year) ? g_daysToMonth366 : g_daysToMonth365;
        if ((day >= 1) && (day <= (numArray[month] - numArray[month - 1])))
        {
            WR32s num = year - 1;
            WR32s num2 = ((((((num * 365) + (num / 4)) - (num / 100)) + (num / 400)) + numArray[month - 1]) + day) - 1;
            m_ticks = (num2 * WRTICKS_PER_DAY);
            return true;
        }
    }
    return false;
}
//构造日期时间。
bool WRDateTime::create(WR32s year, WR32s month, WR32s day, WR32s hour, WR32s minute, WR32s second, WR32s millisecond)
{
    if (year >= 1 && year <= 9999
        && month >= 1 && month <= 12
        && hour >= 0 && hour < 24
        && minute >= 0 && minute < 60
        && second >= 0 && second < 60
        && millisecond >= 0 && millisecond < 1000)
    {
        const WR32s *numArray = isLeapYear(year) ? g_daysToMonth366 : g_daysToMonth365;
        if ((day >= 1) && (day <= (numArray[month] - numArray[month - 1])))
        {
            WR32s num = year - 1;
            WR32s num2 = ((((((num * 365) + (num / 4)) - (num / 100)) + (num / 400)) + numArray[month - 1]) + day) - 1;
            WR32s num3 = hour * WRMILLISECONDS_PER_HOUR + minute * WRMILLISECONDS_PER_MINUTE + second * WRMILLISECONDS_PER_SECOND + millisecond;
            m_ticks = ((num2 * WRTICKS_PER_DAY)+(num3*WRTICKS_PER_MILLISECOND));
            return true;
        }
    }
    return false;
}

//获取本地日期时间。
bool WRDateTime::local()
{
    QDateTime dateTime = QDateTime::currentDateTime();
    QTime time = dateTime.time();
    QDate date = dateTime.date();
    return create(date.year(), date.month(), date.day(), time.hour(), time.minute(), time.second(), time.msec());
}
//获取系统日期时间。
bool WRDateTime::system()
{
    QDateTime dateTime = QDateTime::currentDateTimeUtc();
    QTime time = dateTime.time();
    QDate date = dateTime.date();
    return create(date.year(), date.month(), date.day(), time.hour(), time.minute(), time.second(), time.msec());
}
